<template>
    <div class="improve-formula">
        <el-dialog :title="$t('完善公式')" v-if="dialogVisible" :visible.sync="dialogVisible" width="30%" :before-close="handleClose" :append-to-body="true">
            <div class="improve-formula-content">
                <el-form :model="formData" ref="formData" :rules="rules">
                    <el-row>
                        <el-col :span="8">{{ $t('当: 【') }}{{ bussinessData.groupName }}{{$t('】') }} {{ bussinessData.ruleFieldName }}</el-col>
                        <el-col :span="10">
                            <el-form-item prop="selectValue">
                                <el-select v-model="formData.selectValue" :placeholder="$t('请选择')">
                                    <el-option v-for="item in options" :key="item.value" :label="item.label" :value="item.value"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                    <el-row>
                        <el-col :span="2" style="color: #5086FC;">{{$t('值:') }}</el-col>
                        <el-col :span="16" v-if="setType == 1">
                            <el-form-item prop="inputValue">
                                <el-input v-model="formData.inputValue"></el-input>
                            </el-form-item>
                        </el-col>
                        <el-col :span="16" v-if="setType == 2">
                            <el-form-item prop="paramsValue">
                                <el-select v-model="formData.paramsValue" :placeholder="$t('请选择')">
                                    <el-option v-for="item in paramsOpts" :key="item.value" :label="item.label" :value="item.value"></el-option>
                                </el-select>
                            </el-form-item>
                        </el-col>
                    </el-row>
                </el-form>
            </div>
            <span slot="footer" class="dialog-footer">
                <el-button @click="handleClose">{{ $t('取 消') }}</el-button>
                <el-button type="primary" @click="confirm">{{ $t('确 定') }}</el-button>
			</span>
        </el-dialog>
    </div>
</template>

<script>
import { calcObj } from './common.js'
import api from '@/services/defineContract.js'
import globalService from '@/services/globalService'
export default {
    props: {
        value: Boolean,
        bussinessData: {
            type: Object,
            default: () => {
                return {}
            }   
        },
        row: {
            type: Object,
            default: () => {
                return {}
            }
        },
        rowData: {
            type: Object,
            default: () => {
                return {}
            }
        },
        setType: Number,
    },
    data() {
        return {
            dialogVisible: false,
            options: [],
            paramsOpts: [],
            formData: {},
            rules: {
                selectValue: [{ required: true, message: this.$t('请选择'), trigger: 'change'  }],
                inputValue: [{  required: true, message: this.$t('请输入值'), trigger: 'blur'  }],
                paramsValue: [{  required: true, message: this.$t('请选择'), trigger: 'change'  }],
            },
            calcObj,
        }
    },
    async created() {
      console.log(this.rowData,'rowDatarowDatarowDatarowDatarowData');
        this.dialogVisible = this.value;
        if(this.setType === 1) {
            this.options = [
                { value: 'contains', label: '包含' },
                { value: 'not contains', label: '不包含' },
            ];
        };
        if(this.setType === 2) {
            this.options = [
                { value: 'in', label: 'in' },
                { value: 'not in', label: 'not in' },
            ];
        };
        this.getOpts();
    },
    methods: {
        // 设置下拉值
        async getOpts() {
            // 合同一级二级类目
            if(["firstContractCategory","secondContractCategory"].includes(this.bussinessData.parameterNumber)) {
                const params = {
                    templateTypeCode: this.rowData.templateTypeCode,
                    contractType: this.rowData.contractType,
                    categoryLevel: this.bussinessData.parameterNumber === 'firstContractCategory' ? 1 : 2,
                }
                const res = await api.queryContractCategoryList(params);
                this.paramsOpts = (res.data || []).map((item) => {
                    return {
                        value: item.categoryCode,
                        label: item.categoryName,
                    }
                })  
            } else if(this.bussinessData.parameterNumber == 'contractType') { // 合同类型
                const arr = JSON.parse(this.bussinessData.parameterValueStr);
                this.paramsOpts = (arr || []).map(it => {
                    return {
                        ...it,
                        value: it.code,
                        label: it.name,
                    }
                })
            } else if(this.bussinessData.parameterValueStr) {
                const info = JSON.parse(this.bussinessData.parameterValueStr);
                if (info.enumType == 2) { // 合同关联数据
                    const type = info.relationDataType
                    const templateTypeCode = this.rowData.templateTypeCode
                    const res = await globalService.queryProjectDetailRelatedData({
                        type,
                        templateTypeCode,
                    })
                    this.paramsOpts = (res.data.customerInfoList || []).map((it) => {
                        return {
                            ...it,
                            value: it.customerCode || it.companyCode || it.mainUserId || it.id,
                            label: it.optionName,
                        }
                    })
                } else { // 合同下拉值枚举
                    this.paramsOpts = (info.optionsStr || []).map(it => {
                        return {
                            ...it,
                            value: it.optionName,
                            label: it.optionDesc || it.optionName,
                        }
                    })
                }
            }
        },
        handleClose() {
            this.$emit('input', false);
        },
        getObj(data = []) {
            const obj = data.reduce((acc, cur) => {
                acc[cur.value] = cur.label
                return acc
            }, {});
            return obj || {};
        },
        confirm() {
            this.$refs.formData.validate().then(() => {
                let params = {
                    text: '',
                    value: ''
                };
                const { selectValue, paramsValue, inputValue } = this.formData;
                if (['contains', 'not contains'].includes(selectValue)) {
                    params.text = `${this.bussinessData.ruleFieldName} ${selectValue} '${inputValue}'`;
                    params.value = `${this.bussinessData.ruleFieldKey} ${selectValue} '${inputValue}'`;
                } else if (['in', 'not in'].includes(selectValue)) {
                    const _text = this.getObj(this.paramsOpts)[paramsValue] || '';
                    params.text = `${this.bussinessData.ruleFieldName} ${selectValue} ('${_text}')`;
                    params.value = `${this.bussinessData.ruleFieldKey} ${selectValue} ('${paramsValue}')`;
                }
                this.$emit('improveFormula', params);
                this.handleClose();
            })
        }
    },
}
</script>

<style lang="scss" scoped>
.improve-formula-content {
    line-height: 35px;
    ::v-deep .el-row {
        display: flex;
        justify-content: center;
    }
}
</style>